package com.honey.feature.text;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import com.honey.core.builder.CharacterBuilder;
import com.honey.core.compiler.Compilation;
import com.honey.core.compiler.CompilationException;
import com.honey.core.feature.Feature;
import com.honey.core.utils.HexCode;
import com.honey.feature.BaseFeature;
import com.honey.feature.java.JavaFeature;

/**
 * 文本文件特征抽取. 过程是去掉文件中空白字符然后对有效的文字抽取特征.
 * @author Administrator
 *
 */
public class TextFeature  extends BaseFeature {
	
	//
	private static final Pattern regex = Pattern.compile("\\s") ;
	
	@Override
	public byte[] getContent(Reader reader,String charset) throws Exception {
		byte[] answer = null;
		BufferedReader br = null;
		if( ! (reader instanceof BufferedReader)){
			br = new BufferedReader(reader);
		}else{
			br = (BufferedReader)reader;
		}
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while( (line = br.readLine()) != null){
				sb.append(line).append('\n');
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		String str = sb.toString();
		str = regex.matcher(str).replaceAll("");
		answer = str.getBytes(charset);
		return answer;
	}
	
	public static void main(String args[] ) throws Exception{
		String str ="收到福萨  蒂房设定";
		StringReader r = new StringReader(str);
		TextFeature t = new TextFeature();
		t.getContent(r,"tuf-8");
		System.out.println(r.toString());
		
		
	}
}
